{include file='common/head' /}
<!--百度编辑器-->
<script type="text/javascript" charset="utf-8" src="__UEDITOR__/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="__UEDITOR__/ueditor.all.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<style>
    #shareImg,#shareImg,#coverImg {
        padding:3px;
        border:1px solid #999;
        border-radius: 4px;
        max-width: 400px;
    }
</style>

<!-- 表情开始 -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
<!-- Begin emoji-picker Stylesheets -->
<link href="__PLURING__/emoji/lib/css/nanoscroller.css" rel="stylesheet">
<link href="__PLURING__/emoji/lib/css/emoji.css" rel="stylesheet">
<!-- End emoji-picker Stylesheets -->
<!-- 表情结束 -->

<script type="text/javascript" charset="utf-8" src="__UEDITOR__/lang/zh-cn/zh-cn.js"></script>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <input type="hidden" name="id" value="{$data.id}">
            <div class="layui-form-item">
                <label class="layui-form-label">
                    活动标题
                </label>
                <div class="layui-col-md6">
                    <input type="text" name="title" autocomplete="off" class="layui-input" value="{$data.title}" data-emojiable="true">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    购物券额度
                </label>
                <div class="layui-col-md6">
                    <input type="text" name="initial" autocomplete="off" class="layui-input" value="{$data.initial}" data-emojiable="true">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    购买金额
                </label>
                <div class="layui-col-md6">
                    <input type="text" name="price" autocomplete="off" class="layui-input" value="{$data.price}" data-emojiable="true">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    购买提升等级
                </label>
                <div class="layui-col-md3">
                    <select name="level" lay-verify="required">
                        <option value="0">等级列表</option>
                        {volist name="level" id="v"}
                        <option value="{$v['id']}" {if condition="$v['id'] == $data['level']"}selected{/if} >{$v['name']}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    用户购买后提升至对应等级
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    分享图片
                </label>
                <div class="layui-input-block layui-upload">
                    <button type="button" class="layui-btn layui-btn-normal file" id="share_img_btn"><i class="layui-icon"></i>上传图片</button>
                    <input class="layui-upload-file" type="file" accept="undefined" id="share_img_file">
                    <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                </div>
                <div>
                    <img id="shareImg" src="{$data.share|default=''}" alt="请选择图片" />
                    <input type="hidden" name="share" value="{$data.share|default=''}" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    详情图片
                </label>
                <div class="layui-input-block layui-upload">
                    <button type="button" class="layui-btn layui-btn-normal file" id="detailsImg_img_btn"><i class="layui-icon"></i>上传图片</button>
                    <input class="layui-upload-file" type="file" accept="undefined" id="detailsImg_img_file">
                    <span class="layui-inline layui-upload-choose">支持 png、jpg、jpeg 格式文件</span>
                </div>
                <div>
                    <img id="detailsImg" src="{$data.details_img|default=''}" alt="请选择图片" />
                    <input type="hidden" name="details_img" value="{$data.details_img|default=''}" />
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    活动状态
                </label>
                <div class="layui-col-md6">
                    <input type="radio" name="status" value="1" title="上架" {if condition="$data['status'] eq 1"}checked{/if}>
                    <input type="radio" name="status" value="2" title="下架" {if condition="$data['status'] eq 2"}checked{/if}>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    活动类型
                </label>
                <div class="layui-col-md6">
                    <input type="radio" name="type" value="1" title="常驻" {if condition="$data['type'] eq 1"}checked{/if}>
                    <input type="radio" name="type" value="2" title="限时" {if condition="$data['type'] eq 2"}checked{/if}>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    分享赚升级：分享等级
                </label>
                <div class="layui-col-md3">
                    <select name="share_grade" lay-verify="required">
                        <option value="0">无限制</option>
                        {volist name="level" id="v"}
                        <option value="{$v['id']}" {if condition="$v['id'] == $data['share_grade']"}selected{/if} >{$v['name']}</option>
                        {/volist}
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    达到等级才能获取分享收益
                </div>
            </div>
            <!-- 开始时间 -->
            <div class="layui-form-item">
                <label class="layui-form-label">
                    活动开始时间
                </label>
                <div class="layui-col-md2">
                    <div class="layui-inline layui-show-xs-block">
                        <input class="layui-input"  autocomplete="off" placeholder="开始日期" name="start_time" id="start_time" value="{:date('Y-m-d',$data['start_time'])}">
                    </div>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    活动的开始时间
                </div>
            </div>
            <!-- 结束时间 -->
            <div class="layui-form-item">
                <label class="layui-form-label">
                    活动结束时间
                </label>
                <div class="layui-col-md2">
                    <div class="layui-inline layui-show-xs-block">
                        <input class="layui-input"  autocomplete="off" placeholder="结束日期" name="end_time" id="end_time" value="{if condition="$data['end_time'] neq '0'"}{:date('Y-m-d',$data['end_time'])}{/if}">
                    </div>
                </div>
                <div class="layui-form-mid layui-word-aux">
                    活动的结束时间（不选结束时间则不会结束--即永久有效）
                </div>
            </div>
            <!--<div class="layui-form-item">
                <label class="layui-form-label">
                    分享赚升级：分享人数
                </label>
                <div class="layui-col-md3">
                    <input type="text" name="share_num" autocomplete="off" class="layui-input" value="{$data.share_num}">
                </div>
                <div class="layui-form-mid layui-word-aux">
                    分享人数达标则提升一级，跨等级也只提升一级，分享低等级的则不做提升
                </div>
            </div>-->
            <div class="layui-form-item">
                <label class="layui-form-label">
                    团队累计
                </label>
                <div class="layui-col-md6">
                    <input type="radio" name="is_team" value="1" title="开启" {if condition="$data['is_team'] eq 1"}checked{/if}>
                    <input type="radio" name="is_team" value="2" title="关闭" {if condition="$data['is_team'] eq 2"}checked{/if}>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    分佣方式
                </label>
                <div class="layui-col-md6">
                    <input type="radio" name="is_sale" value="0" title="不开启" {if condition="$data['is_sale'] eq '0'"}checked{/if}>
                    <input type="radio" name="is_sale" value="1" title="百分比" {if condition="$data['is_sale'] eq 1"}checked{/if}>
                    <input type="radio" name="is_sale" value="2" title="固定值" {if condition="$data['is_sale'] eq 2"}checked{/if}>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    分佣数值
                </label>
                <div class="layui-col-md3">
                    <input type="text" name="sale_val" autocomplete="off" class="layui-input" value="{$data.sale_val}">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    活动描述
                </label>
                <div class="layui-input-block">
                    <script id="editor" type="text/plain" style="width:1024px;height:500px;"></script>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">确定保存</button>
            </div>
        </form>
    </div>
</div>
<script>
    //实例化编辑器
    //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
    var ue = UE.getEditor('editor');
    window.onload=function(){
        //不设置页面延迟加载会报错
        setTimeout(function(){
            UE.getEditor('editor').execCommand('insertHtml', '{$data.desc}');
        },2000);
    };

    layui.use(['form', 'layer','jquery','laydate'],function() {
        $ = layui.jquery;
        var form = layui.form,layer = layui.layer;
        var laydate = layui.laydate;
        //日期时间选择器
        laydate.render({
            elem: '#end_time'
        });
        laydate.render({
            elem: '#start_time'
        });

        //监听提交
        form.on('submit(add)', function(data) {
            var num = $('[lay-submit]').index($(this));
            var formData = new FormData($('form')[num]);
            // 更新时间戳
            //formData.set('hours',Math.floor(new Date($("input[name=hours]").val()).getTime() / 1000))
            //发异步，把数据提交给php
            $.ajax({
                url:"{:url('Volumes/volumes_edit')}",
                type: "post",
                data:formData,
                dataType:"json",
                cache: false,                      // 不缓存
                processData: false,                // jQuery不要去处理发送的数据
                contentType: false,                // jQuery不要去设置Content-Type请求头
                success:function (res) {
                    if(res.code == 200){
                        layer.alert(res.msg, {icon: 1},function () {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        });
                    }else{
                        layer.alert(res.msg, {icon: 5});
                    }
                },
                error:function(e){
                    layer.alert("网络错误", {icon: 5});
                },
            });
            return false;
        });

        //图片阅览
        $('#test1').click(function(){
            $('#test1s').click();
        });
        $('#test1s').change(function(e){
            var imgBox = e.target;
            var img = getObjectURL(imgBox.files[0]);
            $('#img').attr('src',img);
        });

        //建立一個可存取到該file的url
        function getObjectURL(file) {
            var url;
            if (window.createObjectURL != undefined) { // basic
                url = window.createObjectURL(file);
            } else if (window.URL != undefined) { // mozilla(firefox)
                url = window.URL.createObjectURL(file);
            } else if (window.webkitURL != undefined) { // webkit or chrome
                url = window.webkitURL.createObjectURL(file);
            }
            return url;
        }

        // 绑定 分享图片
        $("#share_img_btn").click(function(){
            $("#share_img_file").click();
        });

        // 操作成功后的回调 - 活动封面
        $('#share_img_file').change(function(e){
            var imgBox = e.target;
            if(imgBox.files[0].size > 1024 * 1024 *2){
                return layer.msg('失败，文件不得超过2mb',{icon: 5})
            }
            var img = getObjectURL(imgBox.files[0]);
            $('#shareImg').attr('src',img);
            var data = {image: imgBox.files[0]};
            var formData = new FormData();
            formData.append('image', imgBox.files[0]);
            formData.append('prefix','volumes_share');
            $.ajax({
                url:"{:url('admin/uploads/cosFiles')}",
                type: 'post',
                async: false,
                data: formData,
                dataType: 'json',
                processData: false,// 告诉jQuery不要去处理发送的数据
                contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                beforeSend: function (){ //过程...
                },
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg('加载成功', {icon: 6});
                        //$('input[name=img_url]').val(HOST_URL +'/' +  res.data.fileName + '/' + (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                        //$('input[name=img]').val( (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                        $('input[name=share]').val(res.data.url)
                    } else {
                        layer.msg(res.msg || '失败，麻烦重新尝试', {icon: 5})
                    }
                },
                error:function(){
                    layer.msg( '失败，重新尝试，多次失败后麻烦联系开发人员', {icon: 5})
                }
            })
        });


        // 绑定 详情图片
        $("#detailsImg_img_btn").click(function(){
            $("#detailsImg_img_file").click();
        });

        // 操作成功后的回调 - 详情图片
        $('#detailsImg_img_file').change(function(e){
            var imgBox = e.target;
            if(imgBox.files[0].size > 1024 * 1024 *2){
                return layer.msg('失败，文件不得超过2mb',{icon: 5})
            }
            var img = getObjectURL(imgBox.files[0]);
            $('#detailsImg').attr('src',img);
            var data = {image: imgBox.files[0]};
            var formData = new FormData();
            formData.append('image', imgBox.files[0]);
            formData.append('prefix','volumes_details');
            $.ajax({
                url:"{:url('admin/uploads/cosFiles')}",
                type: 'post',
                async: false,
                data: formData,
                dataType: 'json',
                processData: false,// 告诉jQuery不要去处理发送的数据
                contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                beforeSend: function (){ //过程...
                },
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg('加载成功', {icon: 6});
                        //$('input[name=img_url]').val(HOST_URL +'/' +  res.data.fileName + '/' + (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                        //$('input[name=img]').val( (res.data.prefix ? '/' +  res.data.prefix + '/' : '') + res.data.url.replace("\\",'/'))
                        $('input[name=details_img]').val(res.data.url)
                    } else {
                        layer.msg(res.msg || '失败，麻烦重新尝试', {icon: 5})
                    }
                },
                error:function(){
                    layer.msg( '失败，重新尝试，多次失败后麻烦联系开发人员', {icon: 5})
                }
            })
        });
    });
</script>
{include file='common/scroll' /}
{include file='common/footer' /}